﻿/***
	Jlayer.1.1.js
	name:xiaojia
	qq:273142650
	email:iatt@qq.com
	date:2011.9.23
***/
(function () {
		   
	var PATH = document.getElementsByTagName('script')[ document.getElementsByTagName('script').length - 1 ].src;
	var PATH_2 = PATH.split('/')
	var Z_INDEX = 850000;
	var ONLY_Z_INDEX = 0;
	var DIV_ID = 100;
	var ID_DIV;
	var MOVE_POS = 0;
	
	var CALLBACKS = [];
	
	var AUTOARRAY = [];
	
	var SKIN_URL, SKIN_TYPE;
	PATH = PATH.replace(PATH_2[PATH_2.length-1],"");
	SKIN_URL = PATH + 'jlayer_skin-1.1/default.xml';
	
	var IMAGE_ROOT, DEF_WIDTH, DEF_HEIGHT, DEF_TITLE, BORDER, ONLY, STYLE, OPACITY, SUBJECT, AUTO, TITLE;
	
	window.Jlayer = function () {
		return new newJlayer();
	}
	
	var newJlayer = function () {};
	
	newJlayer.prototype = {
		layer: function ($data) {
			this.load($data, 'layer');
		},
		
		alert: function ($data) {
			this.load($data, 'alert');
		},
		
		confirm: function ($data) {
			this.load($data, 'confirm');
		},
		
		load: function ($data, $type) {
			
			this.xml($type);
			
			$model = this.model($data, $type);
			
			document.body.appendChild($model);
			
			AUTO > 0 ? this.auto(DIV_ID) : '';
			
			ONLY != 'true' ? MOVE_POS += 40 : '';
			Z_INDEX ++;
			DIV_ID ++;
		},
		
		skin: function ($skin) {
			SKIN_URL = $skin;
		},
		
		xml: function (type) {
			
			if (window.XMLHttpRequest){
 				xmlhttp = new XMLHttpRequest();
  			} else {
 				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  			}
			
			xmlhttp.open("GET",SKIN_URL,false);
			xmlhttp.send();
			xmlDoc = xmlhttp.responseXML;
			
			xmlDoc = xmlDoc.getElementsByTagName('Jlayer')[0];
			
			IMAGE_ROOT = xmlDoc.getElementsByTagName('image_root')[0].childNodes[0].nodeValue;
			
			xmlDoc = xmlDoc.getElementsByTagName(type)[0];
			
			DEF_WIDTH = xmlDoc.getElementsByTagName('def-width')[0].childNodes[0].nodeValue;
			DEF_HEIGHT = xmlDoc.getElementsByTagName('def-height')[0].childNodes[0].nodeValue;
			DEF_TITLE = xmlDoc.getElementsByTagName('def-title')[0].childNodes[0].nodeValue;
			BORDER = xmlDoc.getElementsByTagName('border')[0].childNodes[0].nodeValue;
			ONLY = xmlDoc.getElementsByTagName('only')[0].childNodes[0].nodeValue;
			OPACITY = xmlDoc.getElementsByTagName('opacity')[0].childNodes[0].nodeValue;
			STYLE = xmlDoc.getElementsByTagName('style')[0].childNodes[0].nodeValue;
			SUBJECT = xmlDoc.getElementsByTagName('subject')[0].childNodes[0].nodeValue;
			TITLE = xmlDoc.getElementsByTagName('title')[0].childNodes[0].nodeValue;
			if (type == 'alert') {
				AUTO = xmlDoc.getElementsByTagName('auto')[0].childNodes[0].nodeValue;
			} else {
				AUTO = 0;
			}
		},
		
		model: function ($data, $type) {
			
			var masks = '';
			var masks_move = 0;
			
			typeof($data) == 'undefined' ? $data = {} : '';
			
			$title = $data.title;
			$content = $data.content;
			$width = $data.width;
			$height = $data.height;
			$top = $data.top;
			$callback = $data.callback;
			$iframe = $data.iframe;
			$only = $data.only;
			$opacity = $data.opacity;
			$auto = $data.auto;
			$type = $type;
			
			typeof($callback) == 'undefined' ? $callback = function (){} : '';
			CALLBACKS[DIV_ID] = $callback;
			
			typeof($width) == 'undefined' ? $width = DEF_WIDTH : '';
			typeof($height) == 'undefined' ? $height = DEF_HEIGHT : '';
			typeof($title) == 'undefined' ? $title = DEF_TITLE : '';
			typeof($content) == 'undefined' ? $content = '' : '';
			typeof($only) != 'undefined' ? ONLY = $only : '';
			typeof($opacity) != 'undefined' ? OPACITY  = $opacity : '';
			typeof($auto) != 'undefined' ? AUTO = $auto : '';
			typeof($top) == 'undefined' ? $top = 150 : '';
			
			$con_width = $width - BORDER * 2;
			$con_height = $height - BORDER * 2;
			$id = 'Jlayer_' + DIV_ID;
			ID_DIV = DIV_ID;
			$left = document.body.clientWidth / 2 - $width / 2;
			$top = $top + document.documentElement.scrollTop + document.body.scrollTop;
			
			switch ($type) {
				case 'layer':
					$style_num = 0;
				break;
				case 'alert':
					$style_num = 1;
				break;
				case 'confirm':
					$style_num = 2;
				break;
			}
			
			STYLE = STYLE.replace(/{width}/ig, $width);
			STYLE = STYLE.replace(/{height}/ig, $height);
			STYLE = STYLE.replace(/{con_width}/ig, $con_width);
			STYLE = STYLE.replace(/{con_height}/ig, $con_height);
			STYLE = STYLE.replace(/{image_root}/ig, IMAGE_ROOT);
			STYLE = STYLE.replace(/{id}/ig, $id)
			
			$style = document.createElement('style');
			$style.id = 'Jlayer_style_' + DIV_ID;
			document.getElementById('Jlayer_style_' + DIV_ID) != null ? document.body.removeChild( document.getElementById('Jlayer_style_' + DIV_ID) ) : '';
			document.body.appendChild($style);
			if (!+"\v1") {
				var $o = STYLE.match(/opacity:(\d?\.\d+);/);
				if ($o != null) {
					STYLE = STYLE.replace($o[0], "filter:alpha(opacity="+ parseFloat($o[1]) * 100+");");
				}
				$style.styleSheet.cssText = STYLE;
			} else {
				$style.innerHTML = STYLE
			}
			
			if (typeof($iframe) != 'undefined') {
				iframeWidth = typeof($iframe.width) == 'undefined' ? $con_width : $iframe.width;
				iframeHeight = typeof($iframe.height) == 'undefined' ? $con_height - TITLE - 1 : $iframe.height;
				$content = '<iframe frameborder="0" id="Jlayer_iframe_'+DIV_ID+'" scrolling="'+$iframe.scroll+'" width="'+iframeWidth+'" height="'+iframeHeight+'" src="'+$iframe.src+'"></iframe>';
			}
			
			SUBJECT = SUBJECT.replace(/{image_root}/ig, IMAGE_ROOT);
			SUBJECT = SUBJECT.replace(/{close}/ig, 'onclick="Jlayer().close(\''+ID_DIV+'\')"');
			SUBJECT = SUBJECT.replace(/{move}/ig, 'onmousedown="Jlayer().move(document.getElementById(\''+$id+'\'), '+$width+', '+$height+');"');
			SUBJECT = SUBJECT.replace(/{ok}/ig, 'onclick="Jlayer().close(\''+ID_DIV+'\');Jlayer().callback('+DIV_ID+');"');
			SUBJECT = SUBJECT.replace(/{auto}/ig, AUTO);
			SUBJECT = SUBJECT.replace(/{closeauto}/ig, 'onclick="Jlayer().closeauto(\''+DIV_ID+'\', this);"');
			SUBJECT = SUBJECT.replace(/{title}/ig, $title);
			SUBJECT = SUBJECT.replace(/{content}/ig, $content);
			SUBJECT = SUBJECT.replace(/{id}/ig, $id);
			
			if (ONLY == 'true') {
				ONLY_Z_INDEX = 1000;
				scrollTop = document.documentElement.scrollTop + document.body.scrollTop;
				$wind_w = document.documentElement.clientWidth;
				$body_h = document.body.offsetHeight;
				$wind_h = document.documentElement.clientHeight;
				if ($body_h < $wind_h) {
					$body_h = $wind_h;
				}
				masks = document.createElement('div');
				masks.style.cssText = ';width:'+$wind_w+'px;height:'+$body_h+'px;position:absolute;top:0;left:0;background:#000;filter:alpha(opacity='+OPACITY * 100+');opacity:'+OPACITY+';z-index:'+parseInt(Z_INDEX + ONLY_Z_INDEX - 1)+'';
				masks.id = 'Jmasks_' + ID_DIV;
				document.body.appendChild(masks);
				masks_move = MOVE_POS;
				document.documentElement.scrollTop = scrollTop;
			} else {
				ONLY_Z_INDEX = 0
			}
			
			$div = document.createElement('div');
			$div.id = $id;
			$div.style.cssText = ';position:absolute;z-index:' + parseInt(Z_INDEX + ONLY_Z_INDEX);
			$div.style.top = $top + MOVE_POS - masks_move + 'px';
			$div.style.left = $left + MOVE_POS - masks_move + 'px';
			$div.innerHTML = SUBJECT;
			
			return $div;
		},
		
		move: function (_this, $w, $h) {
			$drag = true;
			document.onselectstart = function() { return false; } 
			ev = window.event||arguments.callee.caller.arguments[0];
			mousexy = this.mouseXY(ev,_this);
			nowX = mousexy[0];
			nowY = mousexy[1];
			_this.style.zIndex = Z_INDEX + ONLY_Z_INDEX + 1;
			Z_INDEX ++;
			
			movemaskIndex = Z_INDEX + 1;
			movemaskHeight = $h - TITLE - BORDER;
			movemaskTop = parseInt(TITLE) + parseInt(BORDER);
			movemask = document.createElement('div');
			movemask.style.cssText = ';width:'+$w+'px;height:'+movemaskHeight+'px;background:#000;position:absolute;top:'+movemaskTop+'px;left:0;z-index:'+movemaskIndex+';filter:alpha(opacity=0);opacity:0;cursor:move;';
			_this.appendChild(movemask);
			
			document.onmousemove = function () {
				if ($drag) {
					
					$scrollTop = document.documentElement.scrollTop + document.body.scrollTop;
					$scrollLeft = document.documentElement.scrollLeft + document.body.scrollLeft;
					
					$wind_w = document.documentElement.clientWidth + $scrollLeft - $w;
					$wind_h = document.documentElement.clientHeight + $scrollTop - $h;
					
					ev = window.event||arguments.callee.arguments[0];
					divY = ev.clientY - nowY + $scrollTop;
					divX = ev.clientX - nowX + $scrollLeft;
					
					divY < $scrollTop ? divY = $scrollTop : '';
					divX < $scrollLeft ? divX = $scrollLeft : '';
					divY > $wind_h ? divY = $wind_h : '';
					divX > $wind_w ? divX = $wind_w : '';
					
					_this.style.top = divY + 'px';
					_this.style.left = divX + 'px';
					
				}
			}
			document.onmouseup = function () {
				$drag = false;
				_this.removeChild(movemask);
				document.onselectstart = function() { return true; } 
			}
		},
		
		mouseXY: function(e,obj) {
			var X = e.clientX - ( parseInt( obj.style.left ) - document.documentElement.scrollLeft - document.body.scrollLeft );
			var Y = e.clientY - ( parseInt( obj.style.top ) - document.documentElement.scrollTop - document.body.scrollTop );
			return Array(X,Y);
		},
		
		callbacks: function (fn, $id) {
			CALLBACKS[$id] = fn;
		},
		
		callback: function ($id) {
			fn = CALLBACKS[$id];
			CALLBACKS.splice($id,1);
			fn();
		},
		
		auto: function (id) {
			fn = function () {
				Jlayer().close(id);
				if (typeof(CALLBACKS[id]) != 'undefined') {
					Jlayer().callback(id);
				}
			};
			AUTOARRAY[id] = setTimeout(fn, AUTO * 1000);
		},
		
		closeauto: function (id, obj) {
			clearTimeout(AUTOARRAY[id]);
			obj.innerHTML = '';
		},
		
		close: function (id) {
			document.body.removeChild(document.getElementById('Jlayer_'+id));
			if (ONLY == 'true') {
				document.body.removeChild(document.getElementById('Jmasks_'+id));
			} else {
				MOVE_POS -= 40;
			}
		}
	}
})();